//clientCustomer //bindClient //erpcustomer 
$(function(){
	var jTab=$(window.parent.document).find(".J_menuTab.active").text().replace(" ","");
	if(jTab=="商品列表管理管理"||jTab=="门店列表管理管理"){}else{
		 $(".datagrid-pager.pagination.ensureTag").append("<div class='ensureDiv'><a class='easyui-linkbutton l-btn l-btn-small ensure'>确定</a><a class='easyui-linkbutton l-btn l-btn-small cancelBtn'>取消</a></div>")
		 $(".datagrid-pager.pagination.ensureTagPro").append("<div class='ensureDiv'><a class='easyui-linkbutton l-btn l-btn-small ensurePro'>确定</a><a class='easyui-linkbutton l-btn l-btn-small cancelBtn'>取消</a></div>") 
	}

    //取消弹窗
    $(document).on("click",".cancelBtn",function(){
        if( $(window.parent.document).children(".panel-fit").find(".layui-layer-ico.layui-layer-close.layui-layer-close1").length>1){
            $(window.parent.document).children(".panel-fit").children(".easyui-layout.layout.panel-noscroll").children(".layui-layer.layui-layer-iframe").children(".layui-layer-setwin").children(".layui-layer-ico.layui-layer-close.layui-layer-close1")[0].click()
        }       
    })
    //bindstore 弹窗-门店列表管理
	$(document).on("click",".bindstore",function(){
    	UclientId=$(this).parents("tr").children("td[field='clientId']").text()
        UclientName=$(this).parents("tr").children("td[field='clientName']").text()
        Usource=window.location.pathname.replace("/","")
        /*弹窗大小调节*/
		var clientWidth=document.body.clientWidth
		areaArr=[];
		var areaArr0=clientWidth/1.5
		var areaArr1=clientWidth/2.5
		areaArr[0]=areaArr0+'px'
		areaArr[1]=areaArr1+'px'
        var index = layer.open({
            type: 2,
            title: '门店列表管理',
            area: areaArr, //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/erpcustomer?UclientId='+UclientId+'&UclientName='+UclientName+'&Usource='+Usource
        });
        this.layerIndex = index;
        
    })
     //bindpro 弹窗-商品列表管理
     $(document).on("click",".bindpro",function(){
    	UclientId=$(this).parents("tr").children("td[field='clientId']").text()
        UclientName=$(this).parents("tr").children("td[field='clientName']").text()
        Usource=window.location.pathname.replace("/","")
        /*弹窗大小调节*/
		var clientWidth=document.body.clientWidth
		areaArr=[];
		var areaArr0=clientWidth/1.5
		var areaArr1=clientWidth/2.5
		areaArr[0]=areaArr0+'px'
		areaArr[1]=areaArr1+'px'
        var index = layer.open({
            type: 2,
            title: '商品列表管理',
            area: areaArr, //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/erpproduct?UclientId='+UclientId+'&UclientName='+UclientName+'&Usource='+Usource
        });
        this.layerIndex = index;
        
    })
    //clientcustomer页面  绑定客户关联关系
    $(document).on("click",".bindstoreCust",function (){
    	$(this).parent(".datagrid-cell").parent("td[field='action']").parent("tr").addClass("select-bind")
    	$(".iframeCustL").remove();
    	$(".iframeCustR").remove();
    	UclientId=$(this).parents("tr").children("td[field='clientId']").text()
        UclientName=$(this).parents("tr").children("td[field='clientName']").text()
        Usource=window.location.pathname.replace("/","")
        var UorgId=$(this).parent(".datagrid-cell").parent().siblings("td[field='orgId']").text()
        var Usource=window.location.pathname.replace("/","")
        globalUorgId=$(this).parent(".datagrid-cell").parent().siblings("td[field='id']").text();//全局：当前所勾选状态
        console.log(UorgId)
        var unbindCustL="<iframe id='iframeCustL' class='iframeCustL' src="+Feng.ctxPath+"/bindclient?UorgId="+UorgId+"&Usource="+Usource+"></iframe>"
        var unbindCustR="<iframe id='iframeCustR' class='iframeCustR' src="+Feng.ctxPath+"/erpcustomer?UorgId="+UorgId+"&Usource="+Usource+"></iframe>"
        $(".easyui-layout.layout.panel-noscroll").after(unbindCustR);
        $(".easyui-layout.layout.panel-noscroll").after(unbindCustL);
        $(".easyui-layout.layout.panel-noscroll").css({"height":"400px","overflow-y":"scroll"})
        $(".iframeCustL").css({"z-index":"999","position":"absolute","bottom":"0px","width":"35%","height":"428px","overflow":"auto"})
        $(".iframeCustR").css({"z-index":"999","position":"absolute","bottom":"0px","width":"65%","height":"428px","right":"0","overflow":"auto"})
    })
    //clientcustomer页面  删除后刷新
      $(document).on("click",".binddel",function (){
          if(typeof(globalUorgId)!="undefined"){
        	  if($(this).parents("tr").children("td[field='id']").text()==globalUorgId){//删除当前正在绑定
                  $(".search-relative-close")[0].click();
               }
          }
      })
    //bindClient页面、erpcustomer页面   绑定全选 checkbox
    $(".datagrid-cell.datagrid-cell-c1-inputcheck").prepend("<input type='checkbox' class='checkall'/>")
    //全选checkbox
    var countclick=1;
    $(document).on("click",".checkall",function(){
            var isCheck=$(this).is(':checked');  //获得全选复选框是否选中
            $("input[name='storecheck']").each(function(){  
                this.checked = isCheck;       //循环赋值给每个复选框是否选中
            })
    })
    $(document).on("click","input[name=storecheck]",function(){
        if($(this).attr("checked")=="checked"){
            $(this).removeAttr("checked");
        }else{
            $(this).attr("checked","checked");
        }
    })
    function getUrlParameter (param){
        var reg = new RegExp("(^|&)"+ param +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r != null)
            return  decodeURI(r[2]);
        return null;
    }
    //bindClient页面 delmore 多项删除
    $(document).on("click",".delmore",function(){
    	var objclient=[];
    	$("input[name=storecheck]").each(function(){
    		if($(this).is(':checked')){
    			UorgId=$(this).parents("td").siblings("td[field='clientId']").text();
    			orgId=$(this).parents("td").siblings("td[field='bindclientId']").text();
    			var objId={
    					clientId:UorgId,
                    	bindclientId:orgId
    	        }
    			objclient.push(objId)
    		}
    	})
    	console.log(objclient)
    	var ajax = new $ax(Feng.ctxPath +'/bindclient/delByDaos', function (data) {
            //Feng.success("请求成功!");
            $(".l-btn.l-btn-small.l-btn-plain")[5].click();
            $(window.parent.document).find(".refesh-iframe")[0].click();
            
    	}, function (data) {
    		//Feng.error("请求失败!" + data.responseJSON.message + "!");
    	});
    	ajax.set("json",JSON.stringify(objclient));
    	ajax.start();
    })
    
    //erpCustomer页面  点击“确定”ensure
    //绑定门店
    $(document).on("click",".ensure",function(){
        if('clientcustomer'==getUrlParameter("Usource")){
        	if(getUrlParameter("UorgId")==""){
        		UorgIdadd=$(window.parent.document).find(".select-bind").find("td[field='orgId']").text()
        	}else{
        		UorgIdadd=getUrlParameter("UorgId")
        	}
        	console.log(UorgIdadd)
        	var objErpcustomerArr=[];
        	$("input[name=storecheck]").each(function(){
    		if($(this).is(':checked')){
    			orgId=$(this).parents("td").siblings("td[field='orgId']").text();
    			var obj={
                    	clientId:UorgIdadd,
                    	bindclientId:orgId
    	        }
    			objErpcustomerArr.push(obj)
	    		}
	    	})
	    	console.log(objErpcustomerArr)
        	var ajax = new $ax(Feng.ctxPath +'/bindclient/batchadd', function (data) {
                if(data.code==200){
                    Feng.success("请求成功!" +data.msg);
                     //成功后刷新
        			console.log(JSON.stringify(data));
        			$(window.parent.document).find(".refesh-iframe")[0].click();
                }else if(data.code==401){
                	//alert("请求失败!" +data.msg)
                    //Feng.error("请求失败!" +data.msg);
                	 $.messager.show({
                         title:"提示：",
                         msg:"请求失败!" +data.msg,
                         showType:"show",
                         timeout:2000
                     });
//                	$('#win').window({
//                		title:"提示",
//                	    width:300,
//                	    height:150,
//                	    modal:true,
//                	});
//                	$("#win").html("请求失败!" +data.msg)
                }
        	}, function (data) {
        		//Feng.error("请求失败!" + data.responseJSON.message + "!");
        	});
        	ajax.set("json",JSON.stringify(objErpcustomerArr));
        	ajax.start();
        }else{
        	var objErpcustomerArr=[];
        	$("input[name=storecheck]").each(function(){
    		if($(this).is(':checked')){
    			orgId=$(this).parents("td").siblings("td[field='orgId']").text();
    			orgCode=$(this).parents("td").siblings("td[field=orgCode]").text()
    			orgName=$(this).parents("td").siblings("td[field=orgName]").text()
    			var obj={
                	clientId:getUrlParameter("UclientId"),
                	clientName:getUrlParameter("UclientName"),
                    orgId:orgId,
                    orgCode:orgCode,
                    orgName:orgName,
    	        }
    			objErpcustomerArr.push(obj)
	    		}
	    	})
            console.log(objErpcustomerArr)
        	var ajax = new $ax(Feng.ctxPath +'/clientcustomer/batchadd', function (data) {
        		Feng.success("请求成功!");
                $(".l-btn.l-btn-small.l-btn-plain")[4].click();
        	}, function (data) {
        		Feng.error("请求失败!" + data.responseJSON.message + "!");
        	});
        	ajax.set("json",JSON.stringify(objErpcustomerArr));
        	ajax.start();
        }
    })

    //绑定窗口
     $(document).on("click",".ensurePro",function(){
    	console.log(getUrlParameter("UclientId"))
    		var objErpcustomerArr=[];
        	$("input[name=storecheck]").each(function(){
    		if($(this).is(':checked')){
    			productId=$(this).parents("td").siblings("td[field=productId]").text();
    			productCode=$(this).parents("td").siblings("td[field=productCode]").text()
    			productName=$(this).parents("td").siblings("td[field=productName]").text()
    			var obj={
                	clientId:getUrlParameter("UclientId"),
                	clientName:getUrlParameter("UclientName"),
                	productId:productId,
                	productCode:productCode,
                	productName:productName
    	        }
    			objErpcustomerArr.push(obj)
	    		}
	    	})
	    	console.log(objErpcustomerArr)
         var ajax = new $ax(Feng.ctxPath +'/clientproduct/batchadd', function (data) {
             Feng.success("请求成功!");
             $(".l-btn.l-btn-small.l-btn-plain")[4].click();
         }, function (data) {
             Feng.error("请求失败!" + data.responseJSON.message + "!");
         });
         ajax.set("json",JSON.stringify(objErpcustomerArr));
         ajax.start();
    })
    //绑定关联后的取消
    $(document).on("click",".easyui-linkbutton.l-btn.l-btn-small.cancelBtn",function(){
        //console.log($(window.parent.document).find(".refesh-iframe").length)
        if($(window.parent.document).find(".refesh-iframe").length>0){
            $(window.parent.document).find(".search-relative-close")[0].click();
        }
        if($(window.parent.document).find(".layui-layer-ico.layui-layer-close.layui-layer-close1").length>0){
        	$(window.parent.document).find(".layui-layer-ico.layui-layer-close.layui-layer-close1")[0].click();
        }
    })
    //关闭iframe操作
    $(document).on("click",".search-relative-close",function(){
    	$(".iframeCustL").remove();
    	$(".iframeCustR").remove();
    	$(".easyui-layout.layout.panel-noscroll").css({"height":"100%"})
    })
    //refresh iframe 刷新操作
    $(document).on("click",".refesh-iframe",function(){
    	$(this).parent(".datagrid-cell").parent("td[field='action']").parent("tr").addClass("select-bind")
    	$(".iframeCustL").remove();
    	$(".iframeCustR").remove();
    	UclientId=$(this).parents("tr").children("td[field='clientId']").text()
        UclientName=$(this).parents("tr").children("td[field='clientName']").text()
        Usource=window.location.pathname.replace("/","")
        
        var UorgId=$(".select-bind").find("td[field='orgId']").text()
        var Usource=window.location.pathname.replace("/","")
        console.log(UorgId)
        var unbindCustL="<iframe id='iframeCustL' class='iframeCustL' src="+Feng.ctxPath+"/bindclient?UorgId="+UorgId+"&Usource="+Usource+"></iframe>"
        var unbindCustR="<iframe id='iframeCustR' class='iframeCustR' src="+Feng.ctxPath+"/erpcustomer?UorgId="+UorgId+"&Usource="+Usource+"></iframe>"
        $(".easyui-layout.layout.panel-noscroll").after(unbindCustR);
        $(".easyui-layout.layout.panel-noscroll").after(unbindCustL);
        $(".easyui-layout.layout.panel-noscroll").css({"height":"400px","overflow-y":"scroll"})
        $(".iframeCustL").css({"z-index":"999","position":"absolute","bottom":"0px","width":"35%","height":"428px","overflow":"auto"})
        $(".iframeCustR").css({"z-index":"999","position":"absolute","bottom":"0px","width":"65%","height":"428px","right":"0","overflow":"auto"})
     	
    })
    //search-relative
    $(document).on("click",".search-relative",function(){
       var clientId=$(".datagrid-row-selected").children("td[field=clientId]").text()
       var clientName=$(".datagrid-row-selected").children("td[field=clientName]").text()
       var orgId=$(".datagrid-row-selected").children("td[orgId]").text()
       var orgCode=$(".datagrid-row-selected").children("td[orgCode]").text()
       var orgName=$(".datagrid-row-selected").children("td[field=orgName]").text()
       var relativeObj={
            clientId:clientId,
            clientName:clientName,
            orgId:orgId,
            orgCode:orgCode,
            orgName:orgName,
       }
        var index = layer.open({
            type: 2,
            title: '门店列表管理',
            area: ['800px', '560px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/erpcustomer'
        });
        this.layerIndex = index;
    })
    $(document).on("click",".search-relative",function(){
        var clientId=$(".datagrid-row-selected").children("td[field=clientId]").text()
        var clientName=$(".datagrid-row-selected").children("td[field=clientName]").text()
        var orgId=$(".datagrid-row-selected").children("td[field=orgId]").text()
        var orgCode=$(".datagrid-row-selected").children("td[field=orgCode]").text()
        var orgName=$(".datagrid-row-selected").children("td[field=orgName]").text()
        var relativeObj={
             clientId:clientId,
             clientName:clientName,
             orgId:orgId,
             orgCode:orgCode,
             orgName:orgName,
        }
         var index = layer.open({
             type: 2,
             title: '门店列表管理',
             area: ['800px', '560px'], //宽高
             fix: false, //不固定
             maxmin: true,
             content: Feng.ctxPath + '/erpcustomer'
         });
         this.layerIndex = index;
     })

     $(document).on("click",".search-relative-pro",function(){
        var clientId=$(".datagrid-row-selected").children("td[field=clientId]").text()
        var clientName=$(".datagrid-row-selected").children("td[field=clientName]").text()
        var orgId=$(".datagrid-row-selected").children("td[field=productId]").text()
        var orgCode=$(".datagrid-row-selected").children("td[field=productCode]").text()
        var orgName=$(".datagrid-row-selected").children("td[field=productName]").text()
        var relativeObj={
             clientId:clientId,
             clientName:clientName,
             productId:orgId,
             productCode:orgCode,
             productName:orgName,
        }
        console.log('lyy2018-12-23 15:29:22')
     })
     $(".pagination-page-list.pagelistTag").parent().html("<span style='padding:1px 3px'>20条/页<span>")
     $(".pagination-page-list.pagelistTagPro").parent().html("<span style='padding:1px 3px'>20条/页<span>")
     //datagrid-row
     $(document).on("click",".datagrid-row",function(){
        if($(this).find("input[name=storecheck]").length>0){
            var isCheck=$(this).find("input[name=storecheck]").is(':checked');  //是否选中
            if($(this).find("input[name=storecheck]").is(":checked")==true){
                $(this).find("input[name=storecheck]").prop('checked',false);
            }else{
                $(this).find("input[name=storecheck]").prop('checked',true);
            }            
            
        }
     })
})